package com.hrbu.springboot04.interceptor;


import com.hrbu.springboot04.bean.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 添加登录信息拦截器
 * @Author: tanyajun
 * @CreateTime: 2021/4/8
 * @Blog: https://tanyajun.top
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user = (User) request.getSession().getAttribute("USERSESSION");
        if(user!=null){
            if("admin".equals(user.getUsername())&&"admin".equals(user.getPassword())){
                return true;
            }
        }
        request.setAttribute("msg","登录信息失效，请重新登录...");
        request.getRequestDispatcher("/login").forward(request,response);
        return false;
    }
}
